package com.hengpeng.itfin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hengpeng.itfin.domain.draw.DrawProcessorFactory;
import com.hengpeng.itfin.service.DrawFileService;
import com.hengpeng.itfinbase.persist.DrawFile;
import com.hengpeng.itfinbase.persist.DrawFile.DealState;
import com.hengpeng.itfinbase.persist.Drawing;
import com.hengpeng.itfinbase.persist.Drawing.DrawingState;
import com.hengpeng.itfinbase.persist.PaymentProvider;

/**
 * <pre>
 * 用户提款文件服务接口实现
 * </pre>
 * @author shanguogang
 * @version 1.0, 2015-8-12
 */
@Transactional
@Service
@Lazy
public class DrawFileServiceImpl extends BaseServiceImpl implements DrawFileService, ApplicationContextAware
{
	private ApplicationContext context;

	/**
	 * <pre>
	 * 通过文件上传状态获取上传文件列表
	 * </pre>
	 * @param state 文件上传状态
	 * @return 上传文件列表
	 */
	@Override
	public List<DrawFile> listByState(DealState state)
	{
		List<DrawFile> list = serviceFactory.getDaoFactory().getDrawFileDao().listByState(state);
		return list;
	}

	@Override
	@Transactional(propagation = Propagation.NEVER)
	public void requireNeverCreateDrawFile()
	{
		// 获取自动提款支付商列表
		List<PaymentProvider> proList = serviceFactory.getPayService().findProviderAutoPay();
		for (PaymentProvider provider : proList)
		{
			try
			{
				serviceFactory.getDrawFileService().requireNewCreateDrawFile(provider);
			}
			catch (Exception e)
			{
				logger.info("提现支付商{" + provider.getId() + "}创建提款文件异常 ...", e);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.NEVER)
	public void requireNeverDealDrawFile()
	{
		// 获取需要下载的提款结果文件(状态为成功上传的文件)
		List<DrawFile> fileList = serviceFactory.getDrawFileService().listByState(DealState.DOWN_SUCCESS);
		for (DrawFile file : fileList)
		{
			try
			{
				serviceFactory.getDrawFileService().requireNewDealDrawFile(file);
			}
			catch (Exception e)
			{
				logger.info("提现支付商{" + file.getProviderId() + "}处理提款结果文件{" + file.getDown_fileName() + "}异常 ...", e);
			}
		}
	}

	@Transactional(propagation = Propagation.NEVER)
	public void requireNeverQueryDrawFile()
	{
		// 获取需要下载的提款结果文件(状态为成功上传的文件)
		List<DrawFile> fileList = serviceFactory.getDrawFileService().listByState(DealState.UPLOAD_SUCCESS);
		for (DrawFile file : fileList)
		{
			try
			{
				serviceFactory.getDrawFileService().requireNewQueryDrawFile(file);
			}
			catch (Exception e)
			{
				logger.info("提现支付商{" + file.getProviderId() + "}下载提款结果文件{" + file.getUpload_fileName() + "}异常 ...", e);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.NEVER)
	public void requireNeverUploadDrawFile()
	{
		// 获取需要上传的文件
		List<DrawFile> fileList = serviceFactory.getDrawFileService().listByState(DealState.UPLOAD_WAIT);
		for (DrawFile file : fileList)
		{
			try
			{
				serviceFactory.getDrawFileService().requireNewUploadDrawFile(file);
			}
			catch (Exception e)
			{
				logger.info("提现支付商{" + file.getProviderId() + "}上传提款文件{" + file.getUpload_fileName() + "}异常 ...", e);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void requireNewCreateDrawFile(PaymentProvider provider)
	{
		List<Drawing> list = serviceFactory.getDrawingService().getListByState(DrawingState.WAIT, provider.getId());
		if (list != null && list.size() > 0)
		{
			DrawFile createBatchDrawFile = DrawProcessorFactory.getProcessor(context, provider).createBatchDrawFile(
					provider, list);
			if (createBatchDrawFile != null)
			{
				List<Long> drawingIds = new ArrayList<Long>();
				for (Drawing drawing : list)
				{
					drawingIds.add(drawing.getId());
				}
				serviceFactory.getDrawingService().updateState(drawingIds, DrawingState.DEALING);
				serviceFactory.getPersistService().save(createBatchDrawFile);
				logger.info("创 建上传文件 [" + createBatchDrawFile.getUpload_fileName() + "] 成功,共提取数据" + list.size() + "笔.");
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void requireNewDealDrawFile(DrawFile file)
	{
		serviceFactory.getDaoFactory().getCommonDao().lockObject(DrawFile.class, file.getId());
		PaymentProvider provider = serviceFactory.getPersistService().get(PaymentProvider.class, file.getProviderId());
		DrawProcessorFactory.getProcessor(context, provider).parseBatchDrawResultFile(provider, file);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void requireNewQueryDrawFile(DrawFile file)
	{
		serviceFactory.getDaoFactory().getCommonDao().lockObject(DrawFile.class, file.getId());
		PaymentProvider provider = serviceFactory.getPersistService().get(PaymentProvider.class, file.getProviderId());
		DrawProcessorFactory.getProcessor(context, provider).downBatchDrawFileResult(provider, file);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void requireNewUploadDrawFile(DrawFile file)
	{
		serviceFactory.getDaoFactory().getCommonDao().lockObject(DrawFile.class, file.getId());
		PaymentProvider provider = serviceFactory.getPersistService().get(PaymentProvider.class, file.getProviderId());
		DrawFile result = DrawProcessorFactory.getProcessor(context, provider).uploadBatchDrawFile(provider, file);
		if (result != null)
		{
			serviceFactory.getPersistService().update(result);
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException
	{
		this.context = context;
	}
}
